package company.laohu_F;

import java.util.Map;
import java.util.TreeMap;

public class LaoHu1 {

    //关注最高峰需要多少辆车即可
    public int minCarCount(int[][] trips, int capacity) {
        int n = trips.length;
        // 上一个人 下一个人抵消
        Map<Integer, Integer> dict = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            int people = trips[i][0];
            int start = trips[i][1];
            int end = trips[i][2];
            dict.put(start, dict.getOrDefault(start, 0) + people);
            dict.put(end, dict.getOrDefault(start, 0) - people);
        }
        int max = 0, sum = 0;
        for (Map.Entry<Integer, Integer> e : dict.entrySet()) {
            if (e.getValue() >= 0)
                sum += e.getValue();
            else
                sum = 0;
            max = Math.max(max, sum);
        }
        return max / capacity + (max % capacity == 0 ? 0 : 1);
    }
}
